package com.cyk.test6.service

import com.cyk.test6.domain.vo.Address
import com.cyk.test6.domain.vo.Name
import com.cyk.test6.domain.vo.Phone
import com.cyk.test6.repo.SalesRepRepo
import com.cyk.test6.repo.UserRepo
import org.springframework.stereotype.Service

data class User (
    val userId: Long? = null,
    val name: Name,
    val phone: Phone,
    val address: Address,
    val repId: Long? = null,
)

@Service
class UserServiceImpl(
    private val userRepo: UserRepo,
    private val salesRepRepo: SalesRepRepo,
): UserService {

    override fun register(
        name: Name,
        phone: Phone,
        address: Address
    ) {
        //找到区域内的 SalesRep
        val rep: SalesRep? = salesRepRepo.findRep(phone.getAreaCode())

        // 最后创建用户，落盘，然后返回
        val user = User(
            name = name,
            phone = phone,
            address = address,
            repId = rep?.repId
        )

        return userRepo.save(user)
    }

}

data class SalesRep (
    val repId: Long
)